package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.AuditReportService;
import com.yixun.qfbao.apidto.AuditCPReqDto;
import com.yixun.qfbao.apidto.AuditCPResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.enums.SystemIdEnum;
import com.yixun.qfbao.util.WxAccessTokenCacheUtils;
import com.yixun.qfbao.util.exception.TServerException;
import com.yixun.qfbao.vo.AuditCPReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 报备审核
 * @author JHSong
 *
 */
@Service("auditCPHander")
public class AuditCPHander extends CommonAbstractHander {
	private static Logger logger= LoggerFactory.getLogger(AuditCPHander.class);

	@Autowired
	private AuditReportService auditReportService;
    @Autowired
    private WxAccessTokenCacheUtils wxAccessTokenCacheUtils;

	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		AuditCPReqVo req = (AuditCPReqVo)reqDto;
		AuditCPReqDto reqDto1 = req.getParams();
		BaseResponseDTO<AuditCPResDto> res = new BaseResponseDTO<>();
		AuditCPResDto resDto = new AuditCPResDto();
        String assistantAccessToken = null;
        String agencyAccessToken = null;
        try {
            //获取分销助手和案场助手的微信accessToken
            agencyAccessToken = wxAccessTokenCacheUtils.getAccessToken(reqDto1.getCompanyUuid(), SystemIdEnum.AGENCY_h5.getCode(), reqDto1.getDomain());
            assistantAccessToken = wxAccessTokenCacheUtils.getAccessToken(reqDto1.getCompanyUuid(), SystemIdEnum.ASSISTANT.getCode(), reqDto1.getDomain());
			resDto = auditReportService.auditCP(reqDto1,agencyAccessToken,assistantAccessToken);
		}catch(TServerException e){
			resDto.setResult(ResultEnum.FAIL.getNo());
			resDto.setResultMsg(e.getErrorMsg());
		} catch (Exception e) {
			logger.error("AuditCPHander error: {}", e);
			resDto.setResult(ResultEnum.FAIL.getNo());
			resDto.setResultMsg("系统异常");
		}
		res.setResData(resDto);
		res.setResult(resDto.getResult());
		return res;
	}
	
}
